package com.xyazm.order.statemachine;

/**
 * @author xuyou
 * @date 2023年01月15日 19:43
 */

import com.xyazm.common.enums.OrderStatusChangeEnum;
import com.xyazm.common.enums.OrderStatusEnum;
import com.xyazm.order.statemachine.action.OrderActionFactory;
import com.xyazm.order.statemachine.action.OrderStateAction;
import org.squirrelframework.foundation.fsm.annotation.StateMachineParameters;

/**
 * 正向订单状态机配置
 * StateMachineParameters 是squirrel框架提供的，描述订单状态机,进行一些基础性的定义
 * stateType: 定义一个状态机状态是什么类型，OrderStatusEnum
 * eventType: 状态在进行流转的时会发生状态流转事件的类型 orderStatusChange
 * contextType: 在状态流转的时候上下文提供数据
 * 2023-01-15
 * @author xuyou
 * 2023-01-15 19:30
 */
@StateMachineParameters(stateType = OrderStatusEnum.class,eventType = OrderStatusChangeEnum.class,contextType = Object.class)
public class OrderStateMachine extends BaseStateMachine<OrderStatusEnum,OrderStatusChangeEnum> {

    // 状态在流转到不同的状态的时候，就需要不同的action来触发
    private OrderActionFactory orderActionFactory;

    public void setOrderActionFactory(OrderActionFactory orderActionFactory) {
        this.orderActionFactory = orderActionFactory;
    }

    @Override
    protected void onStateChangeInternal(OrderStatusEnum fromStatus, OrderStatusEnum toState, OrderStatusChangeEnum event, Object context) {
        // 真正触发状态流转的动作
        OrderStateAction<?> action = orderActionFactory.getAction(event);
        if (action != null) {
            action.onStateChange(event, context);
        }
    }


    @Override
    public void onStateChange(OrderStatusEnum fromStatus, OrderStatusEnum toStatus, OrderStatusChangeEnum event, Object context) {
        super.onStateChange(fromStatus, toStatus, event, context);
    }
}
